package com.siss.tenancy.user.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class DESUtil {

	public final static String zhiyePrivateKey = "YFGQ$3j#&OJ#D93^7@d)ETg$";

	public final static String autoPrintCardPrivateKey = "Y®Få$8j#&9b#D≈93^7@d)E∆Tg$";
	
	public static void main(String[] args) throws Exception {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("idcards", "450102197707110021");
		String json = jsonObject.toJSONString();
		// 加密
		String sign = DESUtil.aesEncrypt(json, DESUtil.autoPrintCardPrivateKey);
		System.out.println(sign);System.out.println(json);

	}


	public static String aesEncrypt(String str, String privateKey) throws Exception   {
		DESedeKeySpec dks = new DESedeKeySpec(getUtf8Byte(privateKey));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		SecretKey securekey = keyFactory.generateSecret(dks);

		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, securekey);
		byte[] b=cipher.doFinal(getUtf8Byte(str));

		//    BASE64Encoder encoder = new BASE64Encoder();
		//    return encoder.encode(b);
		//    Base64 en = new Base64();
		String encrypt = Base64.encodeBase64String(b);
		return bytesToHexString(getUtf8Byte(encrypt));

	}


	//3DESECB解密,key必须是长度大于等于 3*8 = 24 位
	public static String aesDecrypt(String str, String privateKey) throws Exception  {
		//	System.out.println(src);
		//--通过base64,将字符串转成byte数组
		//    BASE64Decoder decoder = new BASE64Decoder();
		String encry = getUtf8String(hexStringToByte(str));
		byte[] bytesrc = Base64.decodeBase64(encry);// .decodeBuffer(src);
		//--解密的key
		DESedeKeySpec dks = new DESedeKeySpec(getUtf8Byte(privateKey));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		SecretKey securekey = keyFactory.generateSecret(dks);

		//--Chipher对象解密
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, securekey);
		byte[] retByte = cipher.doFinal(bytesrc);

		return getUtf8String(retByte);
	}

	public static String getUtf8String(byte[] src){
		try{
			return new String(src,"utf-8");
		}catch(Exception e){

		}
		return null;
	}

	public static byte[] getUtf8Byte(String src) {
		try{
			return src.getBytes("utf-8");
		}catch(Exception e){

		}
		return null;
	}


	/**
	 * 把字节数组转换成16进制字符串
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}


	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}
}
